﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace DeviceDebugger.Common
{
    /// <summary>
    /// 空值验证
    /// </summary>
    public class NotEnptyValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            if (string.IsNullOrEmpty(value as string) || string.IsNullOrWhiteSpace(value as string))
            {
                return new ValidationResult(false, null);
            }
            else
            {
                return new ValidationResult(true, null);
            }
        }
    }
    /// <summary>
    /// 数字验证
    /// </summary>
    public class NumberValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            if (!string.IsNullOrEmpty(value as string))
            {
                string NumberFormartRegex = @"^\+?[0-9][0-9]*$";

                // 检查输入的字符串是否是数字
                if (Regex.IsMatch(value as string, NumberFormartRegex))
                {
                    return new ValidationResult(true, null);
                }
            }

            return new ValidationResult(false, "只能输入数字");
        }
    }
    /// <summary>
    /// 数字验证
    /// </summary>
    public class IPValidationRule : ValidationRule
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            if (!string.IsNullOrEmpty(value as string))
            {
                string NumberFormartRegex = @"^(([1-9]\d?)|(1\d{2})|(2[01]\d)|(22[0-3]))(\.((1?\d\d?)|(2[04]/d)|(25[0-5]))){3}$";

                // 检查输入的字符串是否是数字
                if (Regex.IsMatch(value as string, NumberFormartRegex))
                {
                    return new ValidationResult(true, null);
                }
            }

            return new ValidationResult(false, "请输入正确的IP地址！");
        }
    }
    /// <summary>
    /// 字符长度验证，介于最短和最长之间，包括最短和最长位数
    /// </summary>
    public class LengthValidationRule : ValidationRule
    {
        public int MaxLength { get; set; }
        public int MinLength { get; set; }
        public string RedTips { get; set; }
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            if (string.IsNullOrEmpty(value as string) || string.IsNullOrWhiteSpace(value as string))
            {
                return new ValidationResult(true, null);
            }
            else
            {
                if ((MinLength == MaxLength && (value as string).Length == MaxLength) || (MinLength != MaxLength && (value as string).Length <= MaxLength && (value as string).Length >= MinLength))
                    return new ValidationResult(true, null);
            }
            return new ValidationResult(false, RedTips);
        }
    }
}
